package com.kid.springcloud.controller;

import com.kid.springcloud.entity.Member;
import com.kid.springcloud.entity.Result;
import com.kid.springcloud.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class MemberController {

    @Resource
    private MemberService memberService;

    @PostMapping("/member/save")
    public Result addMember(@RequestBody Member member) {
        int affected = memberService.addMember(member);
        if (affected > 0) {
            return Result.success("添加会员成功", affected);
        } else {
            return Result.error("401", "添加会员失败");
        }
    }

    @GetMapping("/member/get/{id}")
    public Result queryById(@PathVariable("id") Long id) {
//        //模拟超时5s
//        try {
//            TimeUnit.MILLISECONDS.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        Member member = memberService.getMemberById(id);
        if (member != null) {
            return Result.success("查询成功--10002",member);
        }else {
            return Result.error("404","id="+id+"查找失败");
        }
    }

}
